package com.ykq.service.impl;

import com.ykq.dao.MenuDao;
import com.ykq.entity.Menu;
import com.ykq.entity.Staff;
import com.ykq.service.MenuService;
import com.ykq.util.CommonResult;
import com.ykq.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: qy145-key
 * @description:
 * @author: 闫克起2
 * @create: 2021-12-16 11:41
 **/
@Service(value = "menuService")
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    public CommonResult findByUserRid() {
        Staff staff = (Staff) WebUtil.getSession().getAttribute("staff");
        List<Menu> list = menuDao.findMenuByUid(staff.getRid());

        List<Menu> first=new ArrayList<Menu>();//声明一个集合存放一级菜单
        //找到一级菜单
        for (Menu menu:list){
             if(menu.getPid()==0){
                  first.add(menu);
             }
        }
        //找到对应一级菜单下的二级菜单
        for (Menu f:first){
             List<Menu> children=new ArrayList<Menu>();//当前一级菜单的子菜单
             for(Menu menu:list){
                if(menu.getPid().equals(f.getId())){
                     children.add(menu);
                }
             }
             f.setChildren(children);
        }
        return new CommonResult(2000,"查询左侧菜单成功",first);
    }
}














